package com.shishuo.cms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shishuo.cms.constant.ExceptionConstant;
import com.shishuo.cms.constant.StatusConstant;
import com.shishuo.cms.dao.GoldDao;
import com.shishuo.cms.entity.CashEntity;
import com.shishuo.cms.entity.GoldEntity;
import com.shishuo.cms.entity.UserEntity;
import com.shishuo.cms.entity.vo.PageVo;

@Service
public class GoldService extends BaseService<GoldEntity, Long> {
	private GoldDao goldDao;

	@Autowired
	private UserService userService;
	@Autowired
	private CashService cashService;
	@Autowired
	private ConfigService configService;

	@Autowired
	public void setGoldDao(GoldDao goldDao) {
		super.setBaseDao(goldDao);
		this.goldDao = goldDao;
	}

	public PageVo<GoldEntity> getAllListPage(int pageNum, long userId)
			throws Exception {
		PageVo<GoldEntity> pageVo = new PageVo<GoldEntity>(pageNum);
		List<GoldEntity> list = goldDao.getAllList(pageVo.getOffset(),
				pageVo.getRows(), userId);
		pageVo.setList(list);
		pageVo.setCount(goldDao.getAllListCount(userId));
		return pageVo;
	}

	/**
	 * 用户金币兑换
	 * 
	 * @param id
	 * @param inputGold
	 * @throws Exception
	 */
	@Transactional(rollbackFor = java.lang.Exception.class)
	public void exchangeGold(long userId, double inputGold) throws Exception {
		double goldToMoneyPercent = Double.valueOf(configService
				.getConfigByKey("goldToMoneyPercent"));
		UserEntity userEntityDbEntity = userService.getById(userId);
		if (userEntityDbEntity.getUserGold() < inputGold) {
			throw new Exception(ExceptionConstant.EXCHANGE_GOLD_ERROR);
		}
		double cashCount = inputGold / goldToMoneyPercent;
		userService.addUserGold(userId, 0 - inputGold);// 减金币
		userService.addUserMoney(userId, cashCount);// 加钱
		GoldEntity goldEntity = new GoldEntity();
		goldEntity.setStatus(1);// 金币兑换成功
		goldEntity.setUserId(userId);
		goldEntity.setGoldCount(inputGold);
		goldEntity.setGoldMsg("金币兑换现金");
		goldEntity.setPassMsg("兑换成功");
		goldDao.add(goldEntity);
		CashEntity cashEntity = new CashEntity();
		cashEntity.setCashCount(cashCount);
		cashEntity.setStatus(1);
		cashEntity.setCashMsg("金币兑换现金");
		cashEntity.setPassUserId(1);
		cashEntity.setUserId(userId);
		cashEntity.setPassMsg("兑换成功");
		cashEntity.setCashType(StatusConstant.CASH_TYPE_EXCHANGE);
		cashService.insert(cashEntity);
	}

	@Transactional(rollbackFor = java.lang.Exception.class)
	public void exchangeMoney(long userId, double inputMoney) throws Exception {
		double goldToMoneyPercent = Double.valueOf(configService
				.getConfigByKey("goldToMoneyPercent"));
		UserEntity userEntityDbEntity = userService.getById(userId);
		if (userEntityDbEntity.getUserMoney() < inputMoney) {
			throw new Exception(ExceptionConstant.EXCHANGE_GOLD_ERROR);
		}
		double goldCount = inputMoney * goldToMoneyPercent;
		userService.addUserGold(userId, goldCount);// 加金币
		userService.addUserMoney(userId, 0 - inputMoney);// 减钱
		GoldEntity goldEntity = new GoldEntity();
		goldEntity.setStatus(1);// 金币兑换成功
		goldEntity.setUserId(userId);
		goldEntity.setGoldCount(goldCount);
		goldEntity.setGoldMsg("现金兑换金币");
		goldEntity.setPassMsg("兑换成功");
		goldDao.add(goldEntity);
		CashEntity cashEntity = new CashEntity();
		cashEntity.setCashCount(inputMoney);
		cashEntity.setStatus(1);
		cashEntity.setCashMsg("现金兑换金币");
		cashEntity.setPassUserId(1);
		cashEntity.setUserId(userId);
		cashEntity.setPassMsg("兑换成功");
		cashEntity.setCashType(StatusConstant.CASH_TYPE_EXCHANGE);
		cashService.insert(cashEntity);
	}

	public void addGoldByCallBack(long userId, double goldCount,
			long passUserId, String passMsg) throws Exception {
		GoldEntity goldEntity = new GoldEntity();
		goldEntity.setStatus(1);// 金币返现成功
		goldEntity.setUserId(userId);
		goldEntity.setGoldCount(goldCount);
		goldEntity.setGoldMsg("返利获得金币");
		goldEntity.setPassMsg("用户获得返现!返现订单号为：" + passMsg);
		goldDao.add(goldEntity);
		userService.addUserGold(userId, goldCount);// 加金币
	}
}